-- soul_damage
-- Created by chengb Mar/25/2016
-- 对敌人造成N点伤害，附带灵魂囚笼效果
-- 被杀死的敌人，捕获灵魂；对亡灵及元素等无生命敌人无效

return {
    apply = function(source, target, classId, value)
        -- 传入攻击者信息
        local assailantInfo = {
            ["property_id"] = classId,
        };

        local damage = tonumber(value[1]);

        -- 计算最终伤害
        local damage = PropertyM.calcFinalDamage(source, target, classId, damage);

        CombatM.doDamage(source, target, damage, 0, false, { assailantInfo = assailantInfo, });

        local bonus;
        if target.type == OBJECT_TYPE_MONSTER and target:isDead() and target.dbase:query("soulless") ~= 1 then
            -- 捕获其灵魂
            local rand = DungeonM.getRandSeed("soul_damage") % 1000;
            if rand <= value[2] then
                local soulItem = MonsterM.query(target.classId, "soul_item");
                local prefix = "formula_";
                local itemId;

                if type(soulItem) == "number" then
                    itemId = soulItem;
                elseif type(soulItem) == "string" and
                       string.startWith(soulItem, prefix) then
                    local formulaName = string.sub(soulItem, string.len(prefix) + 1);
                    itemId = FormulaM.invoke(formulaName);
                end

                if type(itemId) == "number" then
                    -- 标记为没有灵魂了
                    target.dbase:set("soulless", 1);

                    bonus = { 1, itemId, 1, };
                    BonusM.doBonus(bonus, "catch_soul");
                end
            end
        end

        -- 抛出道具攻击事件
        EventMgr.fire(event.PROPERTY_ATTACK, {
            ["classId"] = classId,
            ["target"]  = target,
            ["bonus"]   = bonus,
        });
    end,
}